package com.share.user.api.factory;

import com.share.user.api.RemoteUserInfoService;
import com.share.user.api.domain.UserInfo;
import com.share.common.core.domain.R;
import com.share.user.api.domain.UpdateUserLogin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class RemoteUserInfoFallbackFactory implements FallbackFactory<RemoteUserInfoService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteUserInfoFallbackFactory.class);

    @Override
    public RemoteUserInfoService create(Throwable throwable) {
        return new RemoteUserInfoService() {
            @Override
            public R<UserInfo> wxLogin(String code) {
                log.error("远程调用[微信登录]失败，code: {}, 错误: {}", code, throwable.getMessage(), throwable);
                return R.fail("微信登录失败：" + throwable.getMessage());
            }

            @Override
            public R<Boolean> updateUserLogin(UpdateUserLogin updateUserLogin, String source) {
                log.error("远程调用[更新用户登录]失败，用户ID: {}, 来源: {}, 错误: {}",
                        updateUserLogin.getUserId(), source, throwable.getMessage(), throwable);
                return R.fail("更新用户登录失败：" + throwable.getMessage());
            }

            @Override
            public R<UserInfo> getInfo(Long id, String source) {
                log.error("远程调用[微信登录]失败，, 错误: {}", throwable.getMessage(), throwable);
                return R.fail("获取用户信息失败：" + throwable.getMessage());
            }
        };
    }
}